﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using bts_webapp.Models;

namespace bts_webapp
{
    public partial class UxStation : UserControl
    {
        public string StationId {
            get {
                if (ViewState["StationId"] != null)
                    return ViewState["StationId"].ToString();
                return string.Empty;
            }
            set {
                ViewState["StationId"] = value;
            }
        }

        public string Code
        {
            get
            {
                return this.txtStationCode.Text;
            }
            set { this.txtStationCode.Text = value; }
        }

        public string Name
        {
            get
            {
                return this.txtStationName.Text;
            }
            set { this.txtStationName.Text = value; }
        }

        public string Address
        {
            get
            {
                return this.txtAddress.Text;
            }
            set { this.txtAddress.Text = value; }
        }

        public string ResPerson
        {
            get
            {
                return this.txtResUser.Text;
            }
            set { this.txtResUser.Text = value; }
        }

        public string Note
        {
            get
            {
                return this.txtNote.Text;
            }
            set { this.txtNote.Text = value; }
        }

        public bool Status
        {
            get
            {
                return this.chkStatus.Checked;
            }
            set { this.chkStatus.Checked = value; }
        }

        public bool InsertOrUpdate
        {
            set
            {
                this.btnUpdate.CommandName = value ? "PerformInsert" : "Update";
                this.btnUpdate.Text = value ? "Insert" : "Update";
            }
            get
            {
                return this.btnUpdate.Text == "Insert";
            }
        }

        public void EnableFieldCode(bool enabled)
        {
            this.txtStationCode.Enabled = enabled;
        }

        public void BindData(VIBA_TRAM station)
        {
            this.Code = station.Tram_Id;
            this.Name = station.Tram_Ten;
            this.Address = station.Tram_DiaChi;
            this.Note = station.GhiChu;
            this.Status = station.Status == 0? false: true;
            this.ResPerson = station.NguoiPhuTrach;
        }

        public VIBA_TRAM GetData()
        {
            VIBA_TRAM station = new VIBA_TRAM
            {
                Tram_Id = this.InsertOrUpdate ? Guid.NewGuid().ToString(): this.Code,
                Tram_Ten = this.Name,
                NguoiPhuTrach = this.ResPerson,
                Tram_DiaChi = this.Address,
                GhiChu = this.Note,
                Status = GetStatus(this.Status)
            };

            return station;
        }

        private int GetStatus(bool status)
        {
            return status ? 1 : 0;
        }

        public void SetErrorStatus(string error) {
            if (error.Trim() != string.Empty)
                this.litStatus.Text = "<font color='red'>" + error + "</font>";
            else
                this.litStatus.Text = string.Empty;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }


    }
}